package com.qf.front.biz;

import com.qf.ym.entity.GoodsEntity;
import com.qf.ym.entity.GoodsTypeEntity;
import com.qf.ym.mapper.GoodsMapper;
import com.qf.ym.pojo.GoodsPojo;
import com.qf.ym.pojo.GoodsTypePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsBizImpl implements GoodsBiz {
    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public List<GoodsEntity> queryGoodsList(GoodsEntity ge) {

            GoodsPojo gp=entityToPojo(ge);
            List<GoodsPojo> list=goodsMapper.queryGoodsList(gp);
            List<GoodsEntity> list1= new ArrayList<>();
            for(GoodsPojo gp1:list){
               GoodsEntity ge1=pojoToEntity(gp1);
                list1.add(ge1);
             }
            return list1;



    }

    //查询商品类别
    public List<GoodsTypeEntity> queryGoodsTypeList() {


            List<GoodsTypePojo> list = goodsMapper.queryGoodsTypeList();
            List<GoodsTypeEntity> list1 = new ArrayList<>();
            for(GoodsTypePojo gtp :list){
                GoodsTypeEntity gte= typePojoToEntity(gtp);
                list1.add(gte);
            }
            return  list1;


    }

    @Override
    public GoodsEntity queryGoodsDetail(int goodsid) {

         GoodsPojo    gp  =  goodsMapper.queryGoodsDetail(goodsid);
        return  pojoToEntity(gp);
    }

    //商品POJO=>ENTITY
    private GoodsEntity pojoToEntity(GoodsPojo gp){

        GoodsEntity ge = new GoodsEntity();

        ge.setGoodsId(gp.getGid());
        ge.setGoodsName(gp.getGname());
        ge.setGoodsDesc(gp.getGdesc());
        ge.setGoodsPrice(gp.getGprice());
        ge.setGoodsDiscount(gp.getGdiscount());
        ge.setGoodsImage(gp.getGimage());
        ge.setGoodsIsdelete(gp.getIsdelete());
        ge.setGoodsLabel(gp.getGlabel());
        ge.setGoodsSex(gp.getGsex());

        if(null == gp.getGtype()){
            ge.setGoodsType(null);
        }else{
            GoodsTypeEntity gte = typePojoToEntity(gp.getGtype());
            ge.setGoodsType(gte);
        }

        return ge;

    }

    //商品entity=>pojo
    public GoodsPojo entityToPojo(GoodsEntity ge){
        if(ge==null){
            return null;
        }

        GoodsPojo gp = new GoodsPojo();

        gp.setGid(ge.getGoodsId());
        gp.setGdesc(ge.getGoodsDesc());
        gp.setGdiscount(ge.getGoodsDiscount());
        gp.setGimage(ge.getGoodsImage());
        gp.setGlabel(ge.getGoodsLabel());
        gp.setGname(ge.getGoodsName());
        gp.setGprice(ge.getGoodsPrice());
        gp.setGsex(ge.getGoodsSex());
        gp.setIsdelete(ge.getGoodsIsdelete());
        gp.setTid(ge.getGoodsTid());
        if(ge.getGoodsType()==null){
            gp.setGtype(null);
        }else{
            GoodsTypePojo gtp= typeEntityToPojo(ge.getGoodsType());
            gp.setGtype(gtp);
        }
        return gp;
    }

    //商品类别entity=》pojo
    public GoodsTypePojo typeEntityToPojo(GoodsTypeEntity gte) {
        GoodsTypePojo gtp = new GoodsTypePojo();
        gtp.setTid(gte.getTypeId());
        gtp.setTname(gte.getTypeName());
        gtp.setTdesc(gte.getTypeDesc());
        gtp.setTimage(gte.getTypeImage());

        return gtp;
    }

    //商品类别POJO=>entity
    private GoodsTypeEntity typePojoToEntity(GoodsTypePojo gp){
        GoodsTypeEntity gte = new GoodsTypeEntity();

        gte.setTypeId(gp.getTid());
        gte.setTypeDesc(gp.getTdesc());
        gte.setTypeImage(gp.getTimage());
        gte.setTypeName(gp.getTname());

        return gte;
    }
}
